﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using OAWeb.Models;

namespace OAWeb.Controllers
{
    public class StudentsController : Controller
    {
        private OADbContext db = new OADbContext();

        // GET: Students
        public ActionResult Index(int cid , int typeid=0)
        {
            
            ViewBag.cid = cid;
            ViewBag.typeid = typeid;
            var  students  = db.Students.Where(s => s.TeachingClass.TeachingClassID == cid).Include(s => s.StudentType).Include(s => s.TeachingClass);
            if(typeid != 0)
            {
                 students = db.Students.Where(s => s.TeachingClass.TeachingClassID == cid & s.StudentTypeID == typeid).Include(s => s.StudentType).Include(s => s.TeachingClass);
            }

            //List<SelectListItem> listItem = new List<SelectListItem>{
            //     new SelectListItem{Text="正取生",Value="1"},
            //     new SelectListItem{Text="备取生",Value="2"},
            //     new SelectListItem{Text="往届生",Value="3"}
            // };
            //ViewBag.List = new SelectList(listItem, "Value", "Text", "");
            return View(students.ToList());
        }
        //public ActionResult HelperDropDownList()
        //{
        //    List<SelectListItem> listItem = new List<SelectListItem>{
        //         new SelectListItem{Text="正取生",Value="1"},
        //         new SelectListItem{Text="备取生",Value="2"},
        //         new SelectListItem{Text="往届生",Value="3"}
        //     };
        //    ViewBag.List = new SelectList(listItem, "Value", "Text", "");
        //    return View();
        //}
        public ActionResult GetParamsFromFormCollection(FormCollection fc)
        {
            Student student = new Student();

            // int ID = int.Parse(fc["ID"].ToString());
            student.Number = fc["Number"].ToString();
            student.StudentTypeID = int.Parse(fc["StudentTypeID"]);
            student.TeachingClassID = int.Parse(fc["TeachingClassID"]);
            student.Name = fc["Name"].ToString();
            student.Sex = fc["Sex"].ToString();
            student.Nation = fc["Nation"].ToString();
            student.Birthday = DateTime.Parse(fc["Birthday"].ToString());
            student.IDNumber = fc["IDNumber"].ToString();
            
            // student.ID = ID;

            db.Students.Add(student);
            db.SaveChanges();

            return RedirectToAction("Index", new { cid = student.TeachingClassID ,typeid = student.StudentTypeID});
        }
        
        // GET: Students/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Student student = db.Students.Find(id);
            if (student == null)
            {
                return HttpNotFound();
            }
            return View(student);
        }

        // GET: Students/Create
        public ActionResult Create()
        {
            ViewBag.StudentTypeID = new SelectList(db.StudentTypes, "StudentTypeID", "Type");
            ViewBag.TeachingClassID = new SelectList(db.TeachingClasses, "TeachingClassID", "ClassName");
            return View();
        }

        // POST: Students/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ID,Number,TeachingClassID,Name,Sex,Nation,Birthday,IDNumber,StudentTypeID")] Student student)
        {
            if (ModelState.IsValid)
            {
                db.Students.Add(student);
                db.SaveChanges();
                return RedirectToAction("Index", new { cid = student.TeachingClassID });
            }

            ViewBag.StudentTypeID = new SelectList(db.StudentTypes, "StudentTypeID", "Type", student.StudentTypeID);
            ViewBag.TeachingClassID = new SelectList(db.TeachingClasses, "TeachingClassID", "ClassName", student.TeachingClassID);
            return View(student);
        }

        // GET: Students/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Student student = db.Students.Find(id);
            if (student == null)
            {
                return HttpNotFound();
            }
            ViewBag.StudentTypeID = new SelectList(db.StudentTypes, "StudentTypeID", "Type", student.StudentTypeID);
            ViewBag.TeachingClassID = new SelectList(db.TeachingClasses, "TeachingClassID", "ClassName", student.TeachingClassID);
            return View(student);
        }

        // POST: Students/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "ID,Number,TeachingClassID,Name,Sex,Nation,Birthday,IDNumber,StudentTypeID")] Student student)
        {
            if (ModelState.IsValid)
            {
                db.Entry(student).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index", new { cid = student.TeachingClassID });
            }
            ViewBag.StudentTypeID = new SelectList(db.StudentTypes, "StudentTypeID", "Type", student.StudentTypeID);
            ViewBag.TeachingClassID = new SelectList(db.TeachingClasses, "TeachingClassID", "ClassName", student.TeachingClassID);
            return View(student);
        }

        // GET: Students/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Student student = db.Students.Find(id);
            if (student == null)
            {
                return HttpNotFound();
            }
            return View(student);
        }

        // POST: Students/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Student student = db.Students.Find(id);
            db.Students.Remove(student);
            db.SaveChanges();
            return RedirectToAction("Index", new { cid = student.TeachingClassID });
        }
        public ActionResult Information_Depart()
        {
            var departments = db.Departments.ToArray();

            int[] idItems = new int[departments.Length];
            string[] nameItems = new string[departments.Length];
            string[] leaderItems = new string[departments.Length];

            for (int i = 0; i < departments.Length; i++)
            {
                idItems[i] = departments[i].DepartmentID;
                nameItems[i] = departments[i].DepartmentName;
                leaderItems[i] = departments[i].Leader;
            }
            ViewBag.idItems = idItems;
            ViewBag.nameItems = nameItems;
            ViewBag.leaderItems = leaderItems;

            return View(departments);
        }
        public ActionResult Information_DeparGra(int id)
        {

            var departmentGrade = db.DepartmentGrades.Where(d => d.DepartmentID == id).ToArray();

            int[] idItems = new int[departmentGrade.Length];
            string[] nameItems = new string[departmentGrade.Length];
            string[] leaderItems = new string[departmentGrade.Length];
            for (int i = 0; i < departmentGrade.Length; i++)
            {
                nameItems[i] = departmentGrade[i].Name;
                idItems[i] = departmentGrade[i].ID;
                leaderItems[i] = departmentGrade[i].Leader;
            }
            ViewBag.idItems = idItems;
            ViewBag.nameItems = nameItems;
            ViewBag.leaderItems = leaderItems;
            return View();
        }
        public ActionResult Information_Class(int id)
        {

            var teachingClasses = db.TeachingClasses.Where(t => t.DepartmentGradeID == id).ToArray();
            int[] idItems = new int[teachingClasses.Length];
            for (int i = 0; i < teachingClasses.Length; i++)
            {
                idItems[i] = teachingClasses[i].TeachingClassID;
            }
            ViewBag.idItems = idItems;
            return View(teachingClasses);
        }
        public ActionResult Information_Type (int typeid=1)
        {
            var studenttypes = db.StudentTypes.ToArray();

            int[] idItems = new int[studenttypes.Length];
            string[] typeItems = new string[studenttypes.Length];
            
            for (int i = 0; i < studenttypes.Length; i++)
            {
                idItems[i] = studenttypes[i].StudentTypeID;
                typeItems[i] = studenttypes[i].Type;
            }
            ViewBag.idItems = idItems;
            ViewBag.typeItems = typeItems;
           
            return View(studenttypes);
        }
        
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
